import json
from typing import Optional, List
from pydantic import BaseModel
from types import SimpleNamespace

class MessageItem(BaseModel):
    role: str
    content: str

class ResponseChunkChoices(BaseModel):
    index: int
    delta: MessageItem
    finish_reason: str

#对话接口的请求数据格式
class RequestData(BaseModel):
    """对话接口的请求数据格式
    参考OpenAI
    {
        "model": "gpt-3.5-turbo",
        "messages": [
            {
                "role": "system",
                "content": "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown."
            },
            {
                "role": "user",
                "content": "请介绍下你自己？"
            }
        ],
        "max_tokens": 1000,
        "temperature": 0.5,
        "stream": true
    }
    """
    # model: Optional[str]
    bot: Optional[str]
    messages: List[MessageItem]
    # max_tokens: Optional[int]
    temperature: Optional[float]
    stream: Optional[bool]
    # key: Optional[str]
    chatId: str
    # top_p: Optional[int]
    # penalty_score: Optional[float]
    # user_id: Optional[int]


#对话接口的返回数据格式
class ResponseData(BaseModel):
    """对话接口的返回数据格式
    参考OpenAI
    data: {
        "id":"chatcmpl-7c96XWp5jLLe6yFEphcDQne4CHnh3",
        "object":"chat.completion.chunk",
        "created":1689325817,
        "model":"gpt-3.5-turbo-0613",
        "choices":[
            {"index":0,
             "delta":{
                "role":"assistant",
                "content":""},
             "finish_reason":null}]
    }
    """
    id: str
    object: str
    created: int
    model: str
    choices: Optional[List[ResponseChunkChoices]]


class DeepSeekResponse(BaseModel):
    '''
    {'model': 'deepseek-r1:1.5b', 'created_at': '2025-02-06T08:48:19.912128936Z',
     'response': "<think>\n\n</think>\n\nHello! I'm just a virtual assistant, so I don't have feelings, but I'm here and ready to help you with whatever you need. How can I assist you today? 馃槉",
     'done': True,
     'done_reason': 'stop',
     'context': [151644, 9707, 11, 1246, 525, 498, 30, 151645, 151648, 271, 151649, 271, 9707, 0, 358, 2776, 1101, 264,
                 4108, 17847, 11, 773, 358, 1513, 944, 614, 15650, 11, 714, 358, 2776, 1588, 323, 5527, 311, 1492, 498,
                 448, 8820, 498, 1184, 13, 2585, 646, 358, 7789, 498, 3351, 30, 26525, 232],
     'total_duration': 39715377449,
     'load_duration': 2892490748,
     'prompt_eval_count': 9,
     'prompt_eval_duration': 1644000000,
     'eval_count': 44,
     'eval_duration': 35176000000}
'''
    id: int
    model: str
    created_at: str
    response: str
    done: bool



def deepseek2openai(ds:DeepSeekResponse) -> ResponseData:
    if ds.done is False:
        finish_reason = "null"
    else:
        finish_reason = "stop"
    mi = MessageItem(
        role='user',
        content=ds.response
    )
    rcc = ResponseChunkChoices(
        index= ds.id,
        delta = mi,
        finish_reason = finish_reason
    )
    res = ResponseData(
    id = str(ds.id),
    object="chat.completion.chunk",
    created = 20250403,
    model=ds.model,
    choices=[rcc]
    )
    return res

if __name__ == '__main__':
    from pydantic import BaseModel


    class ResponseData(BaseModel):
        field1: str
        field2: int
    # 创建实例
    data = ResponseData(field1="value1", field2=123)
    # 现在可以正常使用 Pydantic 的属性
    print(data.json())